package com.czy.owner.ui.archive;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.TestingRecommendAdapter;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.SelectBespokeListModel;
import com.czy.owner.entity.TestingDetailsModel;
import com.czy.owner.entity.WebViewRequestParam;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.hotshop.GoodsDetailActivity;
import com.czy.owner.ui.order.ProjectDetailsWebviewActivity;
import com.czy.owner.ui.parity.ParityActivity;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.widget.MyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.czy.owner.entity.WebViewRequestParam.REQUEST_POST;

/**
 * 创建2017-11-02 hep 推荐的商品与服务
 **/
public class TestingRecommendActivity extends BaseActivity {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.recyler_goods)
    MyRecyclerView recylerGoods;
    @BindView(R.id.recyler_service)
    MyRecyclerView recylerService;
    TestingRecommendAdapter testingRecommendAdapterService;
    TestingRecommendAdapter testingRecommendAdapterGoods;
    @BindView(R.id.tv_goods_title)
    TextView tvGoodsTitle;
    @BindView(R.id.tv_service_title)
    TextView tvServiceTitle;
    private List<TestingDetailsModel.OrderItemModel> serviceList = new ArrayList<>();
    private List<TestingDetailsModel.OrderItemModel> goodsList = new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_testing_recommend;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        toolbarTitle.setText("推荐商品/服务");
        recylerGoods.setLayoutManager(new LinearLayoutManager(this));
        recylerService.setLayoutManager(new LinearLayoutManager(this));
        setAdapter();
    }

    @Override
    protected void updateViews() {

    }

    public void setAdapter() {
//        List<TestingDetailsModel.OrderItemModel> list = new ArrayList<>();
        goodsList = (List<TestingDetailsModel.OrderItemModel>) getIntent().getSerializableExtra("goodsList");
        serviceList = (List<TestingDetailsModel.OrderItemModel>) getIntent().getSerializableExtra("serviceList");
//        for (TestingDetailsModel.OrderItemModel orderItemModel : list) {
//            if ("service".equals(orderItemModel.getItemType())) {
//                serviceList.add(orderItemModel);
//            } else if ("goods".equals(orderItemModel.getItemType())) {
//                goodsList.add(orderItemModel);
//            }
//        }
        testingRecommendAdapterService = new TestingRecommendAdapter(this);
        testingRecommendAdapterGoods = new TestingRecommendAdapter(this);

        recylerGoods.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, PhoneUtils.dip2px(this, 0.6f), this.getResources().getColor(R.color.divider_list)));
        recylerService.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, PhoneUtils.dip2px(this, 0.6f), this.getResources().getColor(R.color.divider_list)));
        recylerGoods.setAdapter(testingRecommendAdapterGoods);
        recylerService.setAdapter(testingRecommendAdapterService);
        if (goodsList == null || goodsList.size() == 0) {
            tvGoodsTitle.setVisibility(View.GONE);
        } else {
            testingRecommendAdapterGoods.addAll(goodsList);
        }
        if (serviceList == null || serviceList.size() == 0) {
            tvServiceTitle.setVisibility(View.GONE);
        } else {
            testingRecommendAdapterService.addAll(serviceList);
        }

        //推荐的服务
        testingRecommendAdapterService.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<TestingDetailsModel.OrderItemModel>() {
            @Override
            public void onItemClick(View view, int position, TestingDetailsModel.OrderItemModel model) {
                boolean isShow = false;
//                int bindStoreId = (int) SharedPreferencesUtils.getParam(TestingRecommendActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0);
//                if (bindStoreId == Integer.parseInt(StringUtils.getString(model.getStoreId(), "0"))) {
//                    isShow = true;
//                }
                WebViewRequestParam webViewRequestParam = new WebViewRequestParam(Constants.APP_API_BASE + "/own/order/serviceInfo");
                webViewRequestParam.setRequestMethod(REQUEST_POST);
                webViewRequestParam.setParams("session=" + UserHelper.getInstance().getSessionInfoModel(TestingRecommendActivity.
                        this).getSession() + "&storeId=" + model.getStoreId() + "&storeServiceId="
                        + model.getItemTypeId() + "&isDisplayBtn=" + String.valueOf(isShow));
                webViewRequestParam.setTitle("服务详情");
                Intent intent = new Intent(TestingRecommendActivity.
                        this, ProjectDetailsWebviewActivity.class);
                intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                startActivity(intent);


            }
        });
        //推荐的商品

        testingRecommendAdapterGoods.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<TestingDetailsModel.OrderItemModel>() {
            @Override
            public void onItemClick(View view, int position, TestingDetailsModel.OrderItemModel model) {
                boolean isHide = true;
//                int bindStoreId = (int) SharedPreferencesUtils.getParam(TestingRecommendActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0);
//                if (bindStoreId > 0) {
//                    if (bindStoreId != Integer.parseInt(model.getBelongsTypeId())) {
//                        isHide = true;
//                    }
//                } else {
//                    isHide = true;
//                }
                startActivity(new Intent(TestingRecommendActivity.this, GoodsDetailActivity.class).putExtra("goodsId", Integer.parseInt(model.getItemTypeId())).putExtra("isHide", isHide));
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
